package cn.iocoder.yudao.module.nmkj.service.culture;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.nmkj.controller.admin.culture.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.culture.CultureDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nmkj.dal.mysql.culture.CultureMapper;

import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.CULTURE_NOT_EXISTS;

/**
 * 企业文化 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CultureServiceImpl implements CultureService {

    @Resource
    private CultureMapper cultureMapper;

    @Override
    public Long createCulture(CultureSaveReqVO createReqVO) {
        // 插入
        CultureDO culture = BeanUtils.toBean(createReqVO, CultureDO.class);
        cultureMapper.insert(culture);
        // 返回
        return culture.getId();
    }

    @Override
    public void updateCulture(CultureSaveReqVO updateReqVO) {
        // 校验存在
        validateCultureExists(updateReqVO.getId());
        // 更新
        CultureDO updateObj = BeanUtils.toBean(updateReqVO, CultureDO.class);
        cultureMapper.updateById(updateObj);
    }

    @Override
    public void deleteCulture(Long id) {
        // 校验存在
        validateCultureExists(id);
        // 删除
        cultureMapper.deleteById(id);
    }

    private void validateCultureExists(Long id) {
        if (cultureMapper.selectById(id) == null) {
            throw exception(CULTURE_NOT_EXISTS);
        }
    }

    @Override
    public CultureDO getCulture(Long id) {
        return cultureMapper.selectById(id);
    }

    @Override
    public PageResult<CultureDO> getCulturePage(CulturePageReqVO pageReqVO) {
        return cultureMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CultureDO> getCultureList(CulturePageReqVO reqVO) {
        return cultureMapper.selectList(reqVO);
    }

}